package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    //新增课程
    @Log
    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("添加课程信息:{}", course);
        courseService.add(course);
        return Result.success();
    }

    //查询课程列表
    @GetMapping
    public Result list(Integer subject, String name, Integer target,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询课程列表:subject={},name={},label={},page={},pageSize={}", subject, name, target, page, pageSize);
        PageResult<Course> pageResult = courseService.list(subject, name, target, page, pageSize);
        return Result.success(pageResult);
    }

    //根据id查询课程
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询课程的id:{}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    //修改课程信息
    @Log
    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("修改课程信息:{}", course);
        courseService.update(course);
        return Result.success();
    }

    //删除课程
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除课程的id:{}",id);
        courseService.delete(id);
        return Result.success();
    }

    //查询所有课程
    @GetMapping("/list")
    public Result getAll(){
        List<Course> courseList = courseService.getAll();
        return Result.success(courseList);
    }

    //根据学科查询课程
    @GetMapping("/subject/{subject}")
    public Result getBySubject(@PathVariable Integer subject){
        List<Course> courseList = courseService.getBySubject(subject);
        return Result.success(courseList);
    }
}
